<?php

namespace app\sysuser\controller\course;

use app\common\controller\SysBackend;
use app\common\model\banxi\course\Lexicon as LexiconModel;
use app\common\service\ImportService;
use app\common\service\queue\QueueService;
use app\common\service\word\WordService;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

/**
 * 课程字典
 */
class Lexicon extends SysBackend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new LexiconModel();
    }

    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $list = result_row($list);
        $this->ok('操作成功', $list);
    }

    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('查询条件不能为空');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', removeNull($info));
    }

    public function saveOrUpdate()
    {
        $param = $this->request->param();
        try {
            $this->model->saveOrUpdate($param);
            $word = new QueueService();
            $word->addWordSplitJob();
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    public function del()
    {
        $param = $this->request->param(['id']);
        try {
            $validate = Validate::rule([
                'id' => ValidateRule::isRequire(null, '请选择你要删除的记录'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->del($param['id']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    public function dels()
    {
        $param = $this->request->param(['ids', 'courseId']);
        try {
            $validate = Validate::rule([
                'ids' => ValidateRule::isRequire(null, '请选择你要删除的记录'),
                'courseId' => ValidateRule::isRequire(null, '请选择你要删除的课程'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->dels($param['ids'], $param['courseId']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    public function importExcel()
    {

        $curseId = $this->request->param('courseId');
        try {
            $file = $this->request->file('file');
            if (empty($file)) {
                throw new ValidateException('导入失败，请上传需要导入的文件');
            }
            $importService = new ImportService();
            $res = $importService->courseWordImport($file, $curseId);
            if (!$res) {
                throw new ValidateException($importService->getError());
            }
            $word = new QueueService();
            $word->addWordSplitJob();
        } catch (\Exception|ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('导入成功', [], self::NOTIFICATION);

    }
}